/*
 * Copyright 2009 Colin Prinn
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

package net.recursv.motific.unittest;

import java.util.Vector;

/**
 * Created by: recursv
 * Date: 31/07/2009
 * Time: 9:31:03 PM
 * TODO I really don't like this class. It should implemnt a printer interface. And the responsibility for keeping failed test lists should lie elsewhere.
 */
class TestResultPrinter {
    private static final String SEPERATOR = " :: ";
    private static final String TEST_PASSED = "Test Passed :: ";
    private static final String TEST_FAILED = "Test Failed :: ";

    private Vector _list = new Vector();
    private boolean _hasFailedResults = false;
    private int _successfulTests = 0;
    private int _failedTests = 0;
    private final StringBuffer _buffer = new StringBuffer();
    private boolean _printPassedTests = true;
    private boolean _printStackTraces = true;
    private boolean _printFailedTests = true;

    public TestResultPrinter(boolean printStackTraces, boolean printPassedTests, boolean printFailedTests) {
        _printStackTraces = printStackTraces;
        _printPassedTests = printPassedTests;
        _printFailedTests = printFailedTests;
    }


    public void addFailure(Throwable e, String suiteName, String methodName) {
        String msg = _buffer.delete(0, _buffer.length()).append(TEST_FAILED).append(suiteName).append(SEPERATOR).append(methodName).append(SEPERATOR).append(e).toString();
        if (_printFailedTests)
            System.out.println(msg);
        if (_printStackTraces)
            e.printStackTrace();

        _hasFailedResults = true;
        _list.addElement(msg);
        _failedTests++;
    }

    public Vector getFailedResults() {
        return _list;
    }

    public void addSuccess(String suiteName, String methodName) {
        if (_printPassedTests)
            System.out.println(_buffer.delete(0, _buffer.length()).append(TEST_PASSED).append(suiteName).append(SEPERATOR).append(methodName).toString());
        _successfulTests++;
    }

    public boolean hasFailedResults() {
        return _hasFailedResults;
    }

    public int countSuccessfulResults() {
        return _successfulTests;
    }

    public int countFailedResults() {
        return _failedTests;
    }
}
